package com.geezdata.cps.sdk3rd.wechat.method;

import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.SortedMap;
import java.util.UUID;

import com.geezdata.cps.base.utils.Md5Util;

public class WechatPay {
	protected String generateNonceStr() {
		return UUID.randomUUID().toString().toUpperCase().replace("-", "");  
	}
	
	protected String convert2Xml(SortedMap<String, Object> params) {
		StringBuffer sb = new StringBuffer();  
        sb.append("<xml>"); 
        Iterator<Entry<String, Object>> it = params.entrySet().iterator(); 
        while(it.hasNext()) {  
            Map.Entry<String, Object> entry = it.next();  
            String k = (String)entry.getKey();  
            Object v = entry.getValue();
            
            if (v instanceof Number) {
                sb.append("<"+k+">"+v+"</"+k+">");
            } else {
                sb.append("<"+k+">"+"<![CDATA["+v+"]]></"+k+">");
            }
        }  
        
        sb.append("</xml>");  
        return sb.toString(); 
	}
	
	protected String generateSign(SortedMap<String ,Object> params, String payKey){
		String temp = "";
		Iterator<Entry<String, Object>> it = params.entrySet().iterator();  
		while (it.hasNext()){
			Map.Entry<String, Object> entry = it.next();  
			String k = (String)entry.getKey();  
            String v = (String)entry.getValue();
			temp += k + "=" + v + "&";
		}
		
		temp +="key=" + payKey;  
		String sign = Md5Util.md5(temp);
		params.put("sign", sign);
		return sign;
	}
}
